<?php 
class home extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
        function __construct(){
            parent::__construct();
            $this->load->driver('cache');
            $this->load->model('setting_model');
            $this->configs=$this->setting_model->hash();
            $this->load->model('category_model');
            $this->load->model('news_model');
            $this->smarty=new CI_Smarty3();
            $this->smarty->error_reporting =0;// E_ALL & ~E_NOTICE;
        }
        public function index(){
            $assign=null;$data=null;
            $cacheid='home-'.md5(uri_string());
            if($this->config->item('enable_cache'))$data = $this->cache->file->get($cacheid);
            if (!$data){
                $assign['sliders']=$this->category_model->get_by_type('slider');
                //$assign['projects']=$this->news_model->get_by_cate_type($this->configs['10010']);
                $assign['projects']=$this->category_model->get_by_type($this->configs['10010']);
                $assign['customers']=$this->category_model->get_by_type($this->configs['10014']);
                $assign['cate_project']=$this->category_model->get(127);
                $assign['cate_customer']=$this->category_model->get(139);
                $assign['cate_left']=$this->category_model->get(137);
                $assign['cate_right']=$this->category_model->get(138);
                $assign['news_left']=$this->news_model->get_in_cate_ids(137);
                $assign['news_right']=$this->news_model->get_in_cate_ids(138);
                $data=$this->smarty
                    ->assign($assign)
                    ->get_contents('frontend/01_home');
                $this->cache->file->save($cacheid, $data, 300);
            }
            echo $data;
        }
        function about($alias=10008){
            $assign=null;$data=null;
            $cacheid='about-'.md5(uri_string());
            if($this->config->item('enable_cache'))$data = $this->cache->file->get($cacheid);
            if (!$data){
                $assign['danhmucs']=$this->category_model->get_by_type($this->configs['10005']);
                $assign['cate_danhmuc']=$this->category_model->get(10009);
                $assign['news_detail']=$this->news_model->get($alias);
                $data=$this->smarty
                    ->assign($assign)
                    ->get_contents('frontend/02_about');
                $this->cache->file->save($cacheid, $data, 300);
            }
            echo $data;
        }
        function service($cat=null,$alias=null){
            if($cat) $this->service_detail($cat,$alias);
            else $this->service_list($cat);
            
        }
        private function service_list($cat=null){
            $assign=null;$data=null;
            $cacheid='services-'.md5(uri_string());
            if($this->config->item('enable_cache'))$data = $this->cache->file->get($cacheid);
            if (!$data){
                $assign['services']=$this->category_model->get_by_type($this->configs['10009']);
                $assign['cate_service']=$this->category_model->get(130);
                //$assign['news_detail']=$this->news_model->get($alias);
                $data=$this->smarty
                    ->assign($assign)
                    ->get_contents('frontend/03_service');
                $this->cache->file->save($cacheid, $data, 300);
            }
            echo $data;
        }
        private function service_detail($cat=null,$alias=null){
            $assign=null;$data=null;
            $cacheid='service-'.md5(uri_string());
            if($this->config->item('enable_cache'))$data = $this->cache->file->get($cacheid);
            if (!$data){
                $assign['cate_service']=$this->category_model->get_cat_by_alias_type($cat,$this->configs['10009']);
                if(!$assign['cate_service'])show_404 ();
                $assign['services']=$this->news_model->get_in_cate_ids($assign['cate_service']->cate_id);
                if($alias)$assign['news_detail']=$this->news_model->get_by_alias_type($alias,$this->configs['10009']);
                elseif(isset($assign['services'][0]))$assign['news_detail']=$assign['services'][0];
                $data=$this->smarty
                    ->assign($assign)
                    ->get_contents('frontend/04_service_detail');
                $this->cache->file->save($cacheid, $data, 300);
            }
            echo $data;
        }
        function project($cat=null,$alias=null){
            if($alias) $this->project_detail($cat,$alias);
            else $this->project_list($cat);
        }
        private function project_list($cat=null){
            $assign=null;$data=null;
            $cacheid='project-'.md5(uri_string());
            if($this->config->item('enable_cache'))$data = $this->cache->file->get($cacheid);
            if (!$data){
                $assign['projects']=$this->category_model->get_by_type($this->configs['10010']);
                $assign['cate_project']=$assign['projects'][0];
                if(!$cat)$cat=$assign['projects'][0]->cate_alias;
                else{
                    foreach ($assign['projects'] as $pr){
                        if($pr->cate_alias==$cat){
                            $assign['cate_project']=$pr;
                        }
                    }
                }
                $assign['project_list']=$this->news_model->get_in_cate_aliass_by_type($cat,$this->configs['10010']);
                $data=$this->smarty
                    ->assign($assign)
                    ->get_contents('frontend/05_project');
                $this->cache->file->save($cacheid, $data, 300);
            }
            echo $data;
        }
        private function project_detail($cat=null,$alias=null){
            $assign=null;$data=null;
            $cacheid='project-'.md5(uri_string());
            if($this->config->item('enable_cache'))$data = $this->cache->file->get($cacheid);
            if (!$data){
                $assign['projects']=$this->category_model->get_by_type($this->configs['10010']);
                $assign['news_detail']=$this->news_model->get_by_alias_type($alias,$this->configs['10010']);
                $data=$this->smarty
                    ->assign($assign)
                    ->get_contents('frontend/06_project_detail');
                $this->cache->file->save($cacheid, $data, 300);
            }
            echo $data;
        }
        function contact(){
            $assign=null;$data=null;
            $cacheid='project-'.md5(uri_string());
            if($this->config->item('enable_cache'))$data = $this->cache->file->get($cacheid);
            if (!$data){
                
                $data=$this->smarty
                    ->assign($assign)
                    ->get_contents('frontend/07_contact');
                $this->cache->file->save($cacheid, $data, 300);
            }
            echo $data;
        }
        
}
?>